home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / CGI / Push.pm < prev    next >
Encoding:
Perl POD Document  |  1999-12-28  |  6.5 KB  |  224 lines

  1. package CGI::Push;
  2.  
  3.  
  4.  
  5.  
  6.  
  7. $CGI::Push::VERSION='1.00';
  8. use CGI;
  9. @ISA = ('CGI');
  10.  
  11. push(@{$CGI::EXPORT_TAGS{':standard'}},'do_push');
  12.  
  13. sub do_push {
  14.     my ($self,@p) = CGI::self_or_CGI(@_);
  15.  
  16.     $| = 1;
  17.     srand;
  18.     my ($random) = rand()*1E16;
  19.     my ($boundary) = "----------------------------------$random";
  20.  
  21.     my (@header);
  22.     my ($type,$callback,$delay,$last_page,$cookie,$target,$expires,@other) =
  23.     $self->rearrange([TYPE,NEXT_PAGE,DELAY,LAST_PAGE,[COOKIE,COOKIES],TARGET,EXPIRES],@p);
  24.     $type = 'text/html' unless $type;
  25.     $callback = \&simple_counter unless $callback && ref($callback) eq 'CODE';
  26.     $delay = 1 unless defined($delay);
  27.  
  28.     my(@o);
  29.     foreach (@other) { push(@o,split("=")); }
  30.     push(@o,'-Target'=>$target) if defined($target);
  31.     push(@o,'-Cookie'=>$cookie) if defined($cookie);
  32.     push(@o,'-Type'=>"multipart/x-mixed-replace; boundary=$boundary");
  33.     push(@o,'-Server'=>"CGI.pm Push Module");
  34.     push(@o,'-Status'=>'200 OK');
  35.     push(@o,'-nph'=>1);
  36.     print $self->header(@o);
  37.     print "${boundary}$CGI::CRLF";
  38.     
  39.     my @contents;
  40.     while (1) {
  41.     last unless (@contents = &$callback($self,++$COUNTER)) && defined($contents[0]);
  42.     print "Content-type: ${type}$CGI::CRLF$CGI::CRLF";
  43.     print @contents,"$CGI::CRLF";
  44.     print "${boundary}$CGI::CRLF";
  45.     do_sleep($delay) if $delay;
  46.     }
  47.     print "Content-type: ${type}$CGI::CRLF$CGI::CRLF",
  48.           &$last_page($self,++$COUNTER),
  49.           "$CGI::CRLF${boundary}$CGI::CRLF"
  50.           if $last_page && ref($last_page) eq 'CODE';
  51. }
  52.  
  53. sub simple_counter {
  54.     my ($self,$count) = @_;
  55.     return (
  56.         CGI->start_html("CGI::Push Default Counter"),
  57.         CGI->h1("CGI::Push Default Counter"),
  58.         "This page has been updated ",CGI->strong($count)," times.",
  59.         CGI->hr(),
  60.         CGI->a({'-href'=>'http://www.genome.wi.mit.edu/ftp/pub/software/WWW/cgi_docs.html'},'CGI.pm home page'),
  61.         CGI->end_html
  62.         );
  63. }
  64.  
  65. sub do_sleep {
  66.     my $delay = shift;
  67.     if ( ($delay >= 1) && ($delay!~/\./) ){
  68.     sleep($delay);
  69.     } else {
  70.     select(undef,undef,undef,$delay);
  71.     }
  72. }
  73.  
  74. 1;
  75.  
  76. =head1 NAME
  77.  
  78. CGI::Push - Simple Interface to Server Push
  79.  
  80. =head1 SYNOPSIS
  81.  
  82.     use CGI::Push qw(:standard);
  83.  
  84.     do_push(-next_page=>\&next_page,
  85.             -last_page=>\&last_page,
  86.             -delay=>0.5);
  87.  
  88.     sub next_page {
  89.         my($q,$counter) = @_;
  90.         return undef if $counter >= 10;
  91.         return start_html('Test'),
  92.              h1('Visible'),"\n",
  93.                "This page has been called ", strong($counter)," times",
  94.                end_html();
  95.       }
  96.  
  97.      sub last_page {
  98.      my($q,$counter) = @_;
  99.          return start_html('Done'),
  100.                 h1('Finished'),
  101.                 strong($counter),' iterations.',
  102.                 end_html;
  103.      }
  104.  
  105. =head1 DESCRIPTION
  106.  
  107. CGI::Push is a subclass of the CGI object created by CGI.pm.  It is
  108. specialized for server push operations, which allow you to create
  109. animated pages whose content changes at regular intervals.
  110.  
  111. You provide CGI::Push with a pointer to a subroutine that will draw
  112. one page.  Every time your subroutine is called, it generates a new
  113. page.  The contents of the page will be transmitted to the browser
  114. in such a way that it will replace what was there beforehand.  The
  115. technique will work with HTML pages as well as with graphics files, 
  116. allowing you to create animated GIFs.
  117.  
  118. =head1 USING CGI::Push
  119.  
  120. CGI::Push adds one new method to the standard CGI suite, do_push().
  121. When you call this method, you pass it a reference to a subroutine
  122. that is responsible for drawing each new page, an interval delay, and
  123. an optional subroutine for drawing the last page.  Other optional
  124. parameters include most of those recognized by the CGI header()
  125. method.
  126.  
  127. You may call do_push() in the object oriented manner or not, as you
  128. prefer:
  129.  
  130.     use CGI::Push;
  131.     $q = new CGI::Push;
  132.     $q->do_push(-next_page=>\&draw_a_page);
  133.  
  134.         -or-
  135.  
  136.     use CGI::Push qw(:standard);
  137.     do_push(-next_page=>\&draw_a_page);
  138.  
  139. Parameters are as follows:
  140.  
  141. =over 4
  142.  
  143. =item -next_page
  144.  
  145.     do_push(-next_page=>\&my_draw_routine);
  146.  
  147. This required parameter points to a reference to a subroutine responsible for
  148. drawing each new page.  The subroutine should expect two parameters
  149. consisting of the CGI object and a counter indicating the number
  150. of times the subroutine has been called.  It should return the
  151. contents of the page as an B<array> of one or more items to print.  
  152. It can return a false value (or an empty array) in order to abort the
  153. redrawing loop and print out the final page (if any)
  154.  
  155.     sub my_draw_routine {
  156.         my($q,$counter) = @_;
  157.         return undef if $counter > 100;
  158.         return start_html('testing'),
  159.                h1('testing'),
  160.            "This page called $counter times";
  161.     }
  162.  
  163. =item -last_page
  164.  
  165. This optional parameter points to a reference to the subroutine
  166. responsible for drawing the last page of the series.  It is called
  167. after the -next_page routine returns a false value.  The subroutine
  168. itself should have exactly the same calling conventions as the
  169. -next_page routine.
  170.  
  171. =item -type
  172.  
  173. This optional parameter indicates the content type of each page.  It
  174. defaults to "text/html".  Currently, server push of heterogeneous
  175. document types is not supported.
  176.  
  177. =item -delay
  178.  
  179. This indicates the delay, in seconds, between frames.  Smaller delays
  180. refresh the page faster.  Fractional values are allowed.
  181.  
  182. B<If not specified, -delay will default to 1 second>
  183.  
  184. =item -cookie, -target, -expires
  185.  
  186. These have the same meaning as the like-named parameters in
  187. CGI::header().
  188.  
  189. =back
  190.  
  191. =head1 INSTALLING CGI::Push SCRIPTS
  192.  
  193. Server push scripts B<must> be installed as no-parsed-header (NPH)
  194. scripts in order to work correctly.  On Unix systems, this is most
  195. often accomplished by prefixing the script's name with "nph-".  
  196. Recognition of NPH scripts happens automatically with WebSTAR and 
  197. Microsoft IIS.  Users of other servers should see their documentation
  198. for help.
  199.  
  200. =head1 CAVEATS
  201.  
  202. This is a new module.  It hasn't been extensively tested.
  203.  
  204. =head1 AUTHOR INFORMATION
  205.  
  206. be used and modified freely, but I do request that this copyright
  207. notice remain attached to the file.  You may modify this module as you
  208. wish, but if you redistribute a modified version, please attach a note
  209. listing the modifications you have made.
  210.  
  211. Address bug reports and comments to:
  212. lstein@genome.wi.mit.edu
  213.  
  214. =head1 BUGS
  215.  
  216. This section intentionally left blank.
  217.  
  218. =head1 SEE ALSO
  219.  
  220. L<CGI::Carp>, L<CGI>
  221.  
  222. =cut
  223.  
  224.